package main

import (
	"fmt"
	"github.com/grd/stat"
	"math"
)

type IcmpTTL struct{
	Data []float64
}

func (i *IcmpTTL) Get(idx int) float64 {
	if idx < 0 || idx >= len(i.Data) {
		return 0
	}
	return i.Data[idx]
}

func (i *IcmpTTL) Len() int {
	return len(i.Data)
}

func Var(in []float64) float64 {
	var count float64
	var avg float64
	for _, v := range in {
		count = count + v
	}
	fmt.Println("count : ", count)
	avg = count / float64(len(in))
	fmt.Println("avg : ", avg)
	var fx float64
	for _, v := range in {
		t := v - avg
		fx = fx + t * t
	}
	fx = fx / float64(len(in))
	fmt.Println("fx : ", fx)
	var bjx float64
	bjx = math.Sqrt(fx)
	fmt.Println("bjx : ", bjx)

	// 标准差的方差
	var fxUseBjx float64
	for _, v := range in {
		t := v - bjx
		fxUseBjx = fxUseBjx + t * t
	}
	fxUseBjx = fxUseBjx / float64(len(in))
	fmt.Println("fxUseBjx : ", fxUseBjx)
	fmt.Println("bjxUseBjx : ", math.Sqrt(fxUseBjx))
	return bjx
}

func main() {
	Input := []float64{40,40,40,40}
	ttls := IcmpTTL{
		Data: Input,
	}
	// 样本标准差的方差
	ret := stat.Variance(&ttls)
	fmt.Println("ret : ", ret)
	// 样本标准差偏差,
	fmt.Println("bzx : ", math.Sqrt(ret))
}